from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


# 用户表
class UserModel(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
    name = db.Column(db.String(32), comment='用户名')
    mobile = db.Column(db.String(11), comment='手机号')
    role = db.relationship('RoleModel', backref='role')


# 角色表
class RoleModel(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='角色ID')
    name = db.Column(db.String(32), comment='角色名')
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))


# 资源表
class ResourceModel(db.Model):
    __tablename__ = 'resource'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='资源ID')
    name = db.Column(db.String(32), comment='资源名')


# 角色资源表
role_resource = db.Table('role_resource',
                         db.Column('roleid', db.Integer, db.ForeignKey('role.id'), primary_key=True),
                         db.Column('resourceid', db.Integer, db.ForeignKey('resource.id'), primary_key=True)
                         )


# vip表
class VipModel(db.Model):
    __tablename__ = 'vip'
    id = db.Column(db.Integer, primary_key=True, comment='vipID')
    name = db.Column(db.String(32), comment='vip等级')
    condition = db.Column(db.String(128), comment='条件')
    users = db.relationship('UsersModel', backref='users')


# 用户表
class UsersModel(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, comment='用户ID')
    name = db.Column(db.String(32), comment='用户名')
    url = db.Column(db.String(256), comment='头像')
    vip = db.Column(db.String(32), comment='vip等级')
    vip_id = db.Column(db.Integer, db.ForeignKey('vip.id'))


# 标签表
class LabelModel(db.Model):
    __tablename__ = 'label'
    id = db.Column(db.Integer, primary_key=True, comment='标签ID')
    name = db.Column(db.String(32), comment='标签名')


# 用户标签表
user_label = db.Table('user_label',
                      db.Column('usersid', db.Integer, db.ForeignKey('users.id'), primary_key=True),
                      db.Column('labelid', db.Integer, db.ForeignKey('label.id'), primary_key=True)
                      )